<template>
  <div>
    <div>
      <table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: white">
        <tr>
          <td>
            <div style="margin-left: 80px;float: left"><img src="../../assets/logo.jpg" v-on:click="Index"
                                                            height="120px" width="250px"></div>
            <div align="center" style="float: left;margin-left: 200px"><span class="ti"></span></div>
          </td>
        </tr>
      </table>
    </div>
    <div class="register_container"
         style="background-color: white;width: 100%; height:702px;margin-top: -8px ">
      <div
        style="background-color: white;width: 1050px;height: 600px;margin-top: 50px;margin-left:14%;float: left;border-radius: 20px;box-shadow: 1px 2px 3px 4px #cccccc">
        <div style="float:left;margin-left: 50px;margin-top: 60px">
          <img width="500px" src="../../assets/tu2.jpg">
        </div>
        <div class="register_box" style="margin-top: 25px">
          <p style="color: dimgrey;font-family: 黑体;font-size: 40px" align="center">欢迎注册</p>
          <div style="float: left;margin-left: 20px;margin-top: -30px;height: 100px;">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
              <el-form-item label="用户名" prop="userName" class="item">
                <el-input v-model="ruleForm.userName" placeholder="请输入用户名" style="width: 300px"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password" class="item">
                <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="手机号" prop="id" class="item">
                <el-input v-model="ruleForm.phone" placeholder="请输入手机号" style="width: 300px;height: 5px">
                </el-input>
              </el-form-item>
              <el-form-item label="性别" prop="sex" class="item">
                <el-radio v-model="ruleForm.sex" label="男">男</el-radio>
                <el-radio v-model="ruleForm.sex" label="女">女</el-radio>
              </el-form-item>
              <el-form-item label="年龄" prop="age" class="item">
                <el-input v-model="ruleForm.age" placeholder="请输入年龄" style="width: 300px">
                </el-input>
              </el-form-item>
              <el-form-item label="地址" prop="address" class="item">
                <el-input v-model="ruleForm.address" style="width: 300px"></el-input>
              </el-form-item>
              <el-form-item label="邮箱" prop="userEmail" class="item">
                <el-input v-model="ruleForm.userEmail" placeholder="请输入邮箱" style="width: 200px"></el-input>
                <input type="button" id="btnSend" @click="btnSend" value="发送验证码"/>
              </el-form-item>
              <el-form-item label="验证码" prop="code1" class="item">
                <el-input v-model="ruleForm.code1" placeholder="请输入验证码" style="width: 100px"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="registerForm('ruleForm')" align="center">注册</el-button>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <el-link v-on:click="login">立即登录</el-link>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
  export default {
    name: "UserRegister",
    data() {
      var validatePassword = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPassword !== '') {
            this.$refs.ruleForm.validateField('checkPassword');
          }
          callback();
        }
      };
      return {
        ver:'',
        ruleForm: {
          userName: '',
          password: '',
          id: '',
          sex: '女',
          age: '',
          address: '',
          userEmail: '',
          phone: null,
          introduction: '',
          code1:'',
          img5: '',
        },
        rules: {
          userName: [
            {required: true, message: '请输入用户名', trigger: 'blur'},
          ],
          password: [
            {required: true, validator: validatePassword, trigger: 'blur'},
            {min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur'}
          ],
          phone: [
            {required: true, message: '请输入手机号', trigger: 'change'}
          ],
          address: [
            {required: true, message: '请填写收货地址', trigger: 'blur'}
          ],
          userEmail: [
            {required: true, message: '请填写邮箱', trigger: 'blur'}
          ],
          code1: [
            {required: true, message: '请填写验证码', trigger: 'blur'}
          ],

        }
      };
    },
    methods: {
      btnSend() {
        var userEmail = this.ruleForm.userEmail;
        console.log(userEmail);
        this.$http.post('http://localhost:8888/getEmailVerify', {userEmail: userEmail}, {emulateJSON: true}).then(res => {
          console.log(res.data);
          this.ver = res.data;
          this.$message({message:"验证码已发送，请及时查收！",
            type:"success"});

        })
      },
      Index() {
        this.$router.push({path: "/IndexLayout"})
      },
      login() {
        this.$router.push({path: "/UserLogin"})
      },

      registerForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid && this.ver == this.ruleForm.code1) {
            // console.log(456789);
            // console.log(this.ruleForm.phone);
            let img5;
            if(this.ruleForm.sex == '男'){
               img5 ='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fci.xiaohongshu.com%2Fc36c9499-d1fe-3e9d-f55e-8bd21cc8eb62%3FimageView2%2F2%2Fw%2F1080%2Fformat%2Fjpg&refer=http%3A%2F%2Fci.xiaohongshu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1652319430&t=d9157f4a376d8c005c4516b1d0e8a9d9'
            }else{
               img5='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-6bc73af9cd6c0dfbc9377e4c9619ebc5_fhd.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1652257038&t=8fdd803e9bfeb8c6874952045e8f6ac6'
            }
            const {data: res} = await this.$http.post('http://localhost:8888/register',
              {
                userName: this.ruleForm.userName,
                password: this.ruleForm.password,
                id: this.ruleForm.phone,
                sex: this.ruleForm.sex,
                age: this.ruleForm.age,
                address: this.ruleForm.address,
                email: this.ruleForm.userEmail,
                phone: this.ruleForm.phone,
                img:img5 ,
                //img:"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic_source%2Fec%2Fca%2Fd8%2Feccad87a334d082b8ff200ae01157e44.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1652247910&t=3ec61d1c67cdcb26a40fe5a534807064",
                introduction: this.ruleForm.introduction,
              }, {emulateJSON: true});//进入后端
            if (res.code === 200) {
              this.$router.push({path: "/UserLogin"})
            }
          } else {
            alert("验证码输入不正确");
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style>
  .item .el-form-item__label {
    color: dimgrey;
    font-size: 15px;

  }

  .ti {
    font-size: 70px;
    color: rgba(102, 0, 0, 0.82);
    font-family: 微软雅黑
  }

  .register_box {
    color: dimgrey;
    width: 450px;
    height: 480px;
    background-color: rgba(0, 0, 0, 0);
    border-radius: 3px;
    position: absolute;
    left: 52%;
    top: 15%;
    /*transform: translate(-50%, -50%)*/
  }

</style>
